package com.lemon.cloud.redisson.config;

import com.lemon.cloud.lock.DistributedLock;
import com.lemon.cloud.redisson.lock.RedissonDistributedLock;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.redisson.spring.starter.RedissonAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Slf4j
@Configuration
@ConditionalOnClass({RedissonClient.class})
@AutoConfigureAfter(RedissonAutoConfiguration.class)
public class RedissonDistributedLockAutoConfig {
    @Bean
    @ConditionalOnMissingBean
    public DistributedLock redissonDistributedLock(RedissonClient redissonClient) throws IOException {
        RedissonDistributedLock redissonDistributedLock = new RedissonDistributedLock();
        redissonDistributedLock.setRedissonClient(redissonClient);
        log.info("Create a distributed lock RedissonDistributedLock");
        return redissonDistributedLock;
    }
}